using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Web;

namespace ZFrameWork.Util
{
    public class HicnSMS
    {
        private string UserName;
        private string Password;
        private string SubCode;

        public HicnSMS(string username, string password, string subcode)
        {
            UserName = username;
            Password = password;
            SubCode = subcode;
        }

        public string Send(string mobile, string content)
        {
            return Send(mobile, content, DateTime.MinValue);
        }

        public string Send(string mobile, string content, DateTime sendDate)
        {
            string data= "";

            if (sendDate < DateTime.Now)
                data = string.Format("user_id={0}&password={1}&mobile_phone={2}&msg={3}&send_date=&subCode={4}", HttpUtility.UrlEncode(UserName), HttpUtility.UrlEncode(Password), HttpUtility.UrlEncode(mobile), HttpUtility.UrlEncode(content, Encoding.GetEncoding("gb2312")), HttpUtility.UrlEncode(SubCode));
            else
                data = string.Format("user_id={0}&password={1}&mobile_phone={2}&msg={3}&send_date={4}&subCode={5}", HttpUtility.UrlEncode(UserName), HttpUtility.UrlEncode(Password), HttpUtility.UrlEncode(mobile), HttpUtility.UrlEncode(content, Encoding.GetEncoding("gb2312")), sendDate.ToString("yyMMddHHmmss"), HttpUtility.UrlEncode(SubCode));
            string response = PostData("http://bms.hichina.com/sms_gateway/sms_api", data, "");
            if (!string.IsNullOrEmpty(response))
            {
                string[] s = response.Split(new string[] { "\r\n" }, StringSplitOptions.None);
                if (s.Length > 0)
                    return s[0];
                else
                    return "99";
            }
            return "99";
        }


        private string PostData(string url, string data, string referer)
        {
            byte[] dataBytes = Encoding.ASCII.GetBytes(data);
            string content = "";
            HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            //req.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
            //req.Referer = referer;
            //req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; EzCenterExplorer; .NET CLR 2.0.50727)";
            req.ContentLength = dataBytes.Length;
            req.Timeout = 30 * 1000;
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(dataBytes, 0, dataBytes.Length);
            }

            using (HttpWebResponse response = req.GetResponse() as HttpWebResponse)
            {
                StreamReader reader = new StreamReader(response.GetResponseStream());
                content = reader.ReadToEnd();
                reader.Close();
                response.Close();
            }
            return content;
        }
    }
}
